﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class CommunityService : BaseService<Community>, ICommunityService
    {
        private IQueryable<Community> GetJoinAll()
        {
            return GetAll()
                .Include(c => c.Region)
                .Include(c => c.Region.City).AsNoTracking();
        }
        public List<CommunityDTO> GetPagedData(CommunitySearchOptions options, out long total, int pageIndex, int pageSize)
        {
            IQueryable<Community> entities = GetJoinAll();
            if (options.SelectCity != null)
            {
                entities = entities.Where(c => c.Region.City.Id == options.SelectCity);
            }
            if (options.SelectRegion != null)
            {
                entities = entities.Where(c=>c.RegionId == options.SelectRegion);
            }
            if (!string.IsNullOrEmpty(options.Keywords))
            {
                entities = entities.Where(c => c.Name.Contains(options.Keywords)|| c.Location.Contains(options.Keywords)|| c.Traffic.Contains(options.Keywords));
            }

            return base.GetPagedData(entities, out total, pageIndex, pageSize).ToList().Select(r => ToDTO(r)).ToList();
        }

        public CommunityDTO GetDTOById(long id)
        {
            var communities = GetJoinAll().Where(c=>c.Id==id);
            int count = communities.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return ToDTO(communities.Single());
            }
            else
            {
                throw new ApplicationException("获取到多个Id 为" + id + "的小区信息");
            }
        }

        /// <summary>
        /// 根据区域获取小区信息
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public CommunityDTO[] GetByRegionId(long regionId)
        {
            return GetJoinAll().Where(c => c.RegionId == regionId).ToList().Select(c=>ToDTO(c)).ToArray();
        }

        private CommunityDTO ToDTO(Community entity)
        {
            CommunityDTO dto = new CommunityDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.Name = entity.Name;
            dto.RegionId = entity.RegionId;
            dto.RegionName = entity.Region.Name;
            dto.Location = entity.Location;
            dto.Traffic = entity.Traffic;
            dto.BuiltYear = entity.BuiltYear;
            dto.CityId = entity.Region.City.Id;
            dto.CityName = entity.Region.City.Name;
            return dto;
        }

        public void Add(string name, long regionId, string location, string traffic, int? builtYear)
        {
            Community entity = new Community();
            entity.Name = name;
            entity.RegionId = regionId;
            entity.Location = location;
            entity.Traffic = traffic;
            entity.BuiltYear = builtYear;
            Add(entity);
        }
        public void Update(long id, string name, long regionId, string location, string traffic, int? builtYear)
        {
            Community entity = GetById(id);
            if (entity==null)
            {
                throw new Exception("获取不到小区(id:"+id+")信息");
            }
            entity.Name = name;
            entity.RegionId = regionId;
            entity.Location = location;
            entity.Traffic = traffic;
            entity.BuiltYear = builtYear;
            Update(entity);
        }
    }
}
